﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using openCaseAPI;
using System.Xml.Linq;
using OpenQA.Selenium.Chrome;
using System.Threading;

namespace myClient
{
    class Program
    {
        static void Main(string[] args)
        {
            Uri address = new Uri("http://139.196.177.74/");
            runClient rc = new runClient(address);

            //创建两个手机对象
            phone p1 = new phone();
            p1.device = "9790c8a1";//填写你自己的device

            phone p2 = new phone();
            p2.device = "DU2SSE1599007915";//填写你自己的device

            //注册debug事件
            //rc.DebugEvent += p1.Debug;
            rc.DebugEvent += p2.Debug;

            //开启调试模式
            rc.startService();

            Console.ReadLine();//不让控制台退出
        }
    }

    public class phone : phoneAbs
    {
        ChromeDriver ch;
        public override void Debug(object sender, runClient.DebugEventArgs e)
        {
            string info = string.Format("设备{0}启动了测试", this.device);
            Console.WriteLine(info);


            XElement script = e.caseXml;

            var steps = script.Descendants("Step");

            foreach (var step in steps)
            {
                string name = step.Attribute("name").Value;
                switch (name)
                {
                    case "C_InitStep":
                        C_InitStep(step);
                        break;
                    case "C_EditStep":
                        C_EditStep(step);
                        break;
                    default:
                        break;
                }
            }

            Console.WriteLine(string.Format("设备{0}全部执行完成!!!!", this.device));
            Thread.Sleep(10000);//执行太快为了看的清楚一点
            ch.Quit();//记得关闭 chromeDriver
     
        }


        private void C_InitStep(XElement xe)
        {

            ChromeOptions co = new ChromeOptions();
            co.AddAdditionalCapability("androidPackage", "com.android.chrome");
            co.AddAdditionalCapability("androidDeviceSerial", this.device);
            ChromeDriverService cds = ChromeDriverService.CreateDefaultService(System.Environment.CurrentDirectory);

            ch = new ChromeDriver(cds, co);

            //解析xml
            var url = (from t in xe.Descendants("ParamBinding")
                       where t.Attribute("name").Value == "url"
                       select t.Attribute("value").Value).FirstOrDefault();

            ch.Navigate().GoToUrl(url);//打开页面
            Console.WriteLine(string.Format("设备{0}C_InitStep执行完成!!!!", this.device));
        }

        private void C_EditStep(XElement xe)
        {

            //解析xml
            var id = (from t in xe.Descendants("ParamBinding")
                      where t.Attribute("name").Value == "id"
                      select t.Attribute("value").Value).FirstOrDefault();

            var inputText = (from t in xe.Descendants("ParamBinding")
                             where t.Attribute("name").Value == "inputText"
                             select t.Attribute("value").Value).FirstOrDefault();

            var element = ch.FindElementById(id);
            element.Clear();
            element.SendKeys(inputText);
            Console.WriteLine(string.Format("设备{0}C_EditStep执行完成!!!!", this.device));
        }

    }


    
}
